<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue';
import { ElMessage, FormInstance, FormRules, genFileId, UploadFile, UploadFiles } from 'element-plus';
import type { UploadInstance, UploadProps, UploadRawFile } from 'element-plus';
import useStore from '@/store';
import { props } from './basicstockmodify';
import { file } from '@babel/types';
defineOptions({ name: 'BasicStockModify' });
const _props = defineProps(props);
const save = async () => {
  if (!ruleFormRef.value) return;
  await ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      if (fileList.value.length <= 0) {
        ElMessage.error('请选择文件!');
        return;
      }
      form.file = fileList.value[0].raw;
      let params = new FormData();
      for (var key in form) {
        debugger;
        if (form[key].constructor.name == 'Array') {
          for (var f in form[key]) {
            params.append(key, form[key][f]);
          }
        } else {
          params.append(key, form[key]);
        }
      }
      window.$api.stock.basicStockModify(params).then(res => {
        debugger;
        ElMessage.success('保存成功!');
        useStore.window.windowRemove(_props.window?.name);
        if (_props.window?.callBack) _props.window?.callBack();
      });
    }
  });
};
const tableRef = ref(null);
const warehouses = ref(null);
const lenses = ref(null);
const fileList = ref([]);
const actionLogTypes = ref(null);
const actionLogReasons = ref(null);
const ruleFormRef = ref<FormInstance>();
const form = reactive({
  warehouseId: null,
  lensIds: [],
  file: null,
});
const rules = reactive<FormRules>({
  warehouseId: [{ required: true, message: '请选择仓库', trigger: 'change' }],
  lensIds: [{ required: true, message: '请选择镜种', trigger: 'change' }],
});
/** 加载基础数据,字典等 */
const init = async () => {
  await window.$api.warehouse.getWarehouses().then(res => {
    warehouses.value = res.data;
  });
};

const warehouseChange = val => {
  form.warehouseId = val;
  getLenses();
};

const getLenses = () => {
  window.$api.lens.getStockLenses(form.warehouseId).then(res => {
    form.lensIds = [];
    lenses.value = res.data;
  });
};
const lensCodeEnter = () => {
  getLenses();
};

const downLoadXYTemplate = () => {
  if (form.warehouseId == null) {
    ElMessage.error('请选择仓库!');
    return;
  }
  if (form.lensIds == null) {
    ElMessage.error('请选择镜种!');
    return;
  }
  var params = { warehouseId: form.warehouseId, lensIds: form.lensIds };
  //loading.value = true;
  window.$api.stock.downLoadXYTemplate(params).then(res => {
    var aa = res;
    //lenses.value = res.data;
    //loading.value = false;
  });
};

const upload = ref<UploadInstance>();

const handleExceed: UploadProps['onExceed'] = files => {
  upload.value!.clearFiles();
  const file = files[0] as UploadRawFile;
  file.uid = genFileId();
  upload.value!.handleStart(file);
};
const handleFileChange = (uploadFile: UploadFile, uploadFiles: UploadFiles) => {
  //form.file = uploadFile.raw;
};

onMounted(() => {
  init();
  // if (_props.window.params) {
  //   if (_props.window.params.id) {
  //     detail();
  //   }
  // }
});
</script>

<template>
  <windows-window :window="window">
    <template #button>
      <windows-button-group size="18">
        <windows-button icon="fluent-save-28-regular" @click="save"></windows-button>
        <windows-button icon="fluent-contract-down-left-28-regular" @click="downLoadXYTemplate">导出模版</windows-button>
      </windows-button-group>
    </template>
    <el-form ref="ruleFormRef" :model="form" :rules="rules" label-width="100px" class="demo-ruleForm" size="default" status-icon>
      <el-row>
        <el-col :span="12">
          <el-form-item label="仓库" prop="warehouseId">
            <el-select clearable filterable v-model="form.warehouseId" @change="warehouseChange">
              <el-option v-for="item in warehouses" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="镜种" prop="lensIds">
            <el-select clearable filterable multiple v-model="form.lensIds">
              <el-option v-for="item in lenses" :key="item.id" :label="item.factoryLensCode" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="选择文件">
            <el-upload ref="upload" style="width: 100%" drag :limit="1" :auto-upload="false" :on-exceed="handleExceed" :on-change="handleFileChange" v-model:file-list="fileList">
              <el-icon class="el-icon--upload"><upload-filled /></el-icon>
              <div class="el-upload__text">拖动文件到此或者点击选择文件</div>
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </windows-window>
</template>

<style scoped lang="scss">
#edit {
  height: 100%;
}
.el-form {
  margin: 30px 20px;
}
.custom-node {
  display: flex;
  align-items: center;
  span {
    margin-left: 5px;
  }
}
.select-icon {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  border: 1px solid #dcdfe6;
  border-radius: 5px;
  margin-top: 5px;
  background: #fff;
  width: 100%;
  height: 200px;
  overflow: auto;
  .el-image {
    margin: 10px;
    cursor: pointer;
    border-radius: 5px;
    padding: 5px;
    &:hover,
    &.active {
      background: #dcdfe6;
    }
  }
}
</style>
